/**
* 
* ====================================================================
*
* @author apov <apov@ymail.com>
*/ 
// NumericTime.h
//

#pragma once

#include "BaseDefine.h"

/**
* @class NumericTime
*  - 
*/
class DEFLIB_EXTERN_CLASS NumericTime{
	enum{
		LIMIT_YEAR=		9999,
		LIMIT_MONTH=    12,
		LIMIT_HOUR=		23,
		LIMIT_MIN=		59,
		LIMIT_SEC=      59,
		LIMIT_TICK=		1000,
	};

public:
	static void this_sec_to_hour( int source_sec, int& hour, int& min, int& sec );
	static int this_time_sec( int year, int month, int day, int hour, int min, int sec );
	static int getCurrentYear();
	static int getCurrentMonth();
	static int getCurrentDay();
	static int getCurrentHour();
	static int getCurrentMin();
	static int getCurrentSec();
	static int getCurrentWeek();
	static char* getCurrentWeekString();

	static bool is_current_year( size_t year );
	static bool is_current_month( size_t month );
	static bool is_current_day( size_t day );
	
	static bool is_current_day( size_t year, size_t month, size_t day );	

	short getYear();
	char getMonth();
	char getDay();
	char getHour();
	char getMin();
	char getSec();

	unsigned __int64 getNumericTime();

	void setTime(short year, char month, \
		char day, char hour, char min, \
		char sec);
	void setTime(unsigned __int64 numericTime);
	void setTime( const char* time );
	void setTime( NumericTime& time );
	void setTimeString( char* time );
	__int64 setCurrentTime();

	void setYear( short year);
	void setMonth( char month);
	void setDay( char day);
	void setHour( char hour);
	void setMin( char min);
	void setSec( char sec);	

	void addTick( int tick );
	void addSec( char sec );
	void addMin( char min );
	void addHour( char hour );
	void addDay( char day );
	void addMonth( char month );
	void addYear( short year );	
	
	void subTick( int tick );
	void subSec( char sec );
	void subMin( char min );
	void subHour( char hour );
	void subDay( char day );
	void subMonth( char month );
	void subYear( short year );

	int past_min( NumericTime& time );
	static int past_min( NumericTime& small_time, NumericTime& big_time );
	static int past_sec( NumericTime& small_time, NumericTime& big_time );	

	bool isAvailable( unsigned __int64 numericTime );
	char* toString(char* buf, int size);

	void test();

	NumericTime();
	NumericTime( NumericTime& time );
	NumericTime(unsigned __int64 numericTime);
	virtual ~NumericTime();

protected:

private:
	void numericTimeToField(unsigned __int64 numericTime);
	void calcNumericTime();

	char getLimitDay();

	short year_;
	char month_;
	char day_;
	char hour_;
	char min_;
	char sec_;

	unsigned __int64 numericTime_;	
};

#ifdef WIN32
const unsigned __int64 DEF_YEAR_ADD=		10000000000L;
#else
const unsigned __int64 DEF_YEAR_ADD=		10000000000LL;
#endif

const unsigned __int64 DEF_MONTH_ADD=		100000000;
const unsigned __int64 DEF_DAY_ADD=			1000000;
const unsigned __int64 DEF_HOUR_ADD=		10000;
const unsigned __int64 DEF_MIN_ADD=			100;

inline short NumericTime::getYear(){ return year_; }
inline char NumericTime::getMonth(){ return month_; }
inline char NumericTime::getDay(){ return day_; }
inline char NumericTime::getHour(){ return hour_; }
inline char NumericTime::getMin(){ return min_; }
inline char NumericTime::getSec(){ return sec_; }



inline void NumericTime::calcNumericTime(){
	unsigned __int64 year = year_ * DEF_YEAR_ADD;
	unsigned __int64 month = month_ * DEF_MONTH_ADD ;
	unsigned __int64 day = day_ * DEF_DAY_ADD;
	unsigned __int64 hour = hour_ * DEF_HOUR_ADD;
	unsigned __int64 min = min_ * DEF_MIN_ADD;
	numericTime_ = (year + month + day + hour + min + sec_);
}

inline unsigned __int64 NumericTime::getNumericTime(){ return numericTime_; }

